//g++ prefetch_test.cpp
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <xmmintrin.h>

#include "ipp.h"//Ipp64u
#define Matrix_Len 1024
#define Loop 500

int main()
{
    clock_t t;
    Ipp64u start,end;

    int a[Matrix_Len][Matrix_Len];

    t = clock();
    start = ippCoreGetCpuClocks();

    for(long long loop = 0; loop < Loop; ++loop)
    {
        for(int i = 0; i < Matrix_Len; ++i)
        {
            for(int j = 0; j < Matrix_Len; ++j)
            {
                a[i][j] = 1;
            }
        }
    }
    
    printf("Gap1: %ld\n", clock() - t);


    t = time(NULL);
    for(long long loop = 0; loop < Loop; ++loop)
    {    
        for(int i = 0; i < Matrix_Len; ++i)
        {
            for(int j = 0; j < Matrix_Len; ++j)
            {   //几乎无空间局部性可言，造成程序性能低下。
                a[j][i] = 1;
            }
        }
    }
    printf("Gap2:%ld\n", clock() - t);

    t = clock();
    for(long long loop = 0; loop < Loop; ++loop)
    {    
        for(int i = 0; i < Matrix_Len; ++i)
        {
            for(int j = 0; j < Matrix_Len; ++j)
            {   //通过软预取，可提升一定程度性能。
                _mm_prefetch(&a[j + 1][i], _MM_HINT_T0);
                a[j][i] = 1;
            }
        }
    }
    printf("Gap3:%ld\n", clock() - t);

    return 0;
}
